/*************************************************
Author: byron
Date: 11/19/2013
Description:
Notes: 
*************************************************/

#ifndef __CManorBattleMgr_h__
#define __CManorBattleMgr_h__

#include "CTArray.h"
#include "commclass.h"
#include "CManorBattleCfg.h"
#include "CManorBattleData.h"

class CDropItem;
class CActCfgData;
class MsgManorBattleRank;
class MsgReqManorBattle;
class MsgAnsManorBattle;
class CBattleRoleList;
class CActStatusCfgData;
class CPlayerOnlineData;
class CPlayerCacheData;

class CManorBattleUnit
{
public:
    CManorBattleUnit()
    {
        Clear();
    }

    void Clear()
    {
        _bIsBreak = false;
        _bNotifySelf = true;
        _iSubCmd = 0;
        _iTargetCamp = 0;
        _iStatForMon = 0;
        _uiMainGID = 0;
        _uiMapID = 0;
    }

    virtual ~CManorBattleUnit()
    {
    }

public:
    bool _bIsBreak;
    bool _bNotifySelf;
    int _iSubCmd;
    int _iTargetCamp;
    int _iStatForMon;
    unsigned int _uiMainGID;
    unsigned int _uiMapID;
};

class CManorBattleInfo
{
public:
    CManorBattleInfo()
    {
        Clear();
    }

    void Clear()
    {
        _stUnitList.Clear();
    }

    virtual ~CManorBattleInfo()
    {
    }

    int GetUnitSize() const {return _stUnitList.Size();}
    CManorBattleUnit& GetUnit(int iIndex) {return _stUnitList[iIndex];}
    void AddUnit(CManorBattleUnit& stUnit) {_stUnitList.AddOneItem(stUnit);}

private:
    CTLib::CTArray<CManorBattleUnit, MAX_MANOR_BATTLE_INFO_COUNT> _stUnitList;
};

class CManorBattleMgr
{
public:
    enum CMD_MANOR_BATTLE_SUB_TAG
    {
        CMD_MANOR_BATTLE_SUB_INFO = 1,            //获取信息
        CMD_MANOR_BATTLE_SUB_TARGET = 2,          //目标列表
        CMD_MANOR_BATTLE_SUB_BATTLE = 3,          //进入战斗
        CMD_MANOR_BATTLE_SUB_REVIVE = 4,          //角色复活
        CMD_MANOR_BATTLE_SUB_DEAD = 5,            //角色死亡
        CMD_MANOR_BATTLE_SUB_SCENE_INFO = 6,      //用于后端主动下发场景信息
        CMD_MANOR_BATTLE_SUB_MONSTER = 7,         //用于后端主动下发怪物信息
        CMD_MANOR_BATTLE_SUB_MAP_SHOW = 8,        //获取进入地图前显示信息
        CMD_MANOR_BATTLE_SUB_RANK = 9,            //用于后端主动下发各排名榜
        CMD_MANOR_BATTLE_SUB_LEAVE = 10,          //离开目标队列
        CMD_MANOR_BATTLE_SUB_SCENE_OVER = 11,     //当前场景战斗结束 MsgManorBattleShow fight_map 地图表示跳转
        CMD_MANOR_BATTLE_SUB_TIME_OVER = 12,      //活动到期结束

        CMD_INNER_MANOR_BATTLE_LEAVE = 101,       //角色离开地图
    };

    enum ENUM_MANOR_BATTLE_MACRO_TAG
    {
        MANOR_BATTLE_STAT_ONE = 0,        //战斗阶段 -- 黑铁之堡
        MANOR_BATTLE_STAT_TWO = 1,        //战斗阶段 -- 白银之角
        MANOR_BATTLE_STAT_THR = 2,        //战斗阶段 -- 黄金之城

        MANOR_BATTLE_LEVEL_ONE = 4,       //世界等级偏移 -- 黑铁之堡
        MANOR_BATTLE_LEVEL_TWO = 2,       //世界等级偏移 -- 白银之角
        MANOR_BATTLE_LEVEL_THR = 0,       //世界等级偏移 -- 黄金之城

        MANOR_BATTLE_PREPARE_SEC = 60,    //战斗阶段准备时长
        MANOR_BATTLE_DEAD_WAIT_TIME = 30, //死亡冷却时长
        MANOR_BATTLE_FIGHT_WAIT_TIME = 10,//战斗冷却时长

        MANOR_BATTLE_MONSTER_OBJ_GID = 1,       //攻击怪物的宏
        MANOR_BATTLE_SCORE_LEVEL_RATIO = 2,     //等级倍率
        MANOR_BATTLE_SCORE_MONSTER_RATIO = 2,   //怪物倍率
        MANOR_BATTLE_SCORE_ATTACK_RATIO = 1300, //当玩家为进攻方时系数
        MANOR_BATTLE_SCORE_DEFEND_RATIO = 1000, //当玩家为防守方时系数
        MANOR_BATTLE_SCORE_DIVISOR_RATIO = 1000,//计算积分时的除数倍率

        MANOR_BATTLE_BLESS_STATUS_ONE = 10001,   //黑铁护佑状态ID
        MANOR_BATTLE_BLESS_STATUS_TWO = 10002,   //白银护佑状态ID
        MANOR_BATTLE_BLESS_STATUS_THR = 10003,   //黄金护佑状态ID
        MANOR_BATTLE_BOMB_STATUS = 10011,  //飞火流星1级状态ID
        MANOR_BATTLE_BOMB_LEVEL_MAX = 29,  //存储的值域为[0,29]

        MANOR_BATTLE_ITEM_ROLE_RANK = 100, //个人积分三强榜奖励
        MANOR_BATTLE_ITEM_MON_KILL_ONE = 201, //黑铁之堡城门怪击杀奖励
        MANOR_BATTLE_ITEM_MON_KILL_TWO = 202, //白银之角城门怪击杀奖励
        MANOR_BATTLE_ITEM_MON_KILL_THR = 203, //黄金之城城门怪击杀奖励
        MANOR_BATTLE_ITEM_BELONG_ONE = 301, //黑铁之堡领地归属奖励
        MANOR_BATTLE_ITEM_BELONG_TWO = 302, //白银之角领地归属奖励
        MANOR_BATTLE_ITEM_BELONG_THR = 303, //黄金之城领地归属奖励
        MANOR_BATTLE_ITEM_ATTACK_CLAN = 401, //进攻方家族积分排名奖励起始

        MANOR_BATTLE_TITLE_LAST_TIME = (3600 * 23 + 1800), //称号持续时间
    };

    CManorBattleMgr()
    {
        Clear();
    }

    void Clear()
    {
        _stRank.Clear();
        _stData.Clear();
    }

    virtual ~CManorBattleMgr()
    {
    }

    void SetMap();
    bool IsValidStatForMon();
    bool IsValidStatForMon(int iStatForMon);
    bool IsValidStatForMap();
    bool IsValidStatForMap(int iStatForMap);
    bool IsManorBattleScene(const unsigned int uiMapID);
    bool IsRoleInManorBattle(unsigned int uiGID);
    bool IsMapIDNeedUpdateDB(const unsigned int uiMapID);

    bool IsDefendListGID(unsigned int uiGID)
    {
        return _stRank.IsDefendListGID(uiGID);
    }
    bool IsAttackListGID(unsigned int uiGID)
    {
        return _stRank.IsAttackListGID(uiGID);
    }

    int GetDefendListSize() const {return _stRank.GetDefendListSize();}
    int GetAttackListSize() const {return _stRank.GetAttackListSize();}

    CManorBattleDefTag& GetDefendTag(int iIndex)
    {
        return _stRank.GetDefendTag(iIndex);
    }
    CManorBattleAtkTag& GetAttackTag(int iIndex)
    {
        return _stRank.GetAttackTag(iIndex);
    }

    int GetStatEndTime() const {return _stData.GetStatEndTime();}
    int GetClanCamp(unsigned int uiClanID)
    {
        return _stData.GetClanCamp(uiClanID);
    }

    int ActOpen(CActCfgData& stActCfgData);

    int ActClose(CActCfgData& stActCfgData);

    int EnterScene(unsigned int& uiMapID, unsigned short& usPosX,
            unsigned short& usPosY, CPlayerDetailInfo& stPlayerDetail);
   
    int LeaveScene(unsigned int uiGID);

    int LeaveScene(CPlayerDetailInfo& stPlayerDetail, CManorBattleUnit& stUnit);

    int CheckManorBattle(CPlayerCacheData& stPlayerCache);

    int ManorBattle(CPlayerBaseInfo& stPlayerBaseA,
            CPlayerDetailInfo& stPlayerDetailA,
            CPlayerOnlineData& stPlayerOnlineA,
            CPlayerCacheData& stPlayerCacheA, CPlayerBaseInfo& stPlayerBaseB,
            CPlayerDetailInfo& stPlayerDetailB,
            CPlayerOnlineData& stPlayerOnlineB,
            CPlayerCacheData& stPlayerCacheB, CManorBattleInfo& stInfo,
            bool bIsAttackRole, int iRoleACamp, MsgReqManorBattle& stReq,
            MsgAnsManorBattle& stAns, MsgAnsManorBattle& stObjAns);

    int ReviseBeforeBattle(CPlayerCacheData& stPlayerCache,
            CBattleRoleList& stRoleList, bool bIsDefend);

    int ResetHPAfterBattle(CPlayerCacheData& stPlayerCache,
            CBattleRoleList& stRoleList);

    void DealStatusAfterBattle(CPlayerCacheData& stPlayerCache,
            CActStatusCfgData& stActStatusCfg);

    void CalcBattleScore(CPlayerBaseInfo& stPlayerBaseA,
            CPlayerBaseInfo& stPlayerBaseB, bool bIsAttackRole, bool bIsAWin,
            int iRoleACamp, int& iScoreA, int& iScoreB);

    void CalcAttackMonReward(int iAttackVal, int iMonMaxHP, int iWorldLevel,
            CDropItem& stDropItem);

    void SetBattleTip(CPlayerBaseInfo& stPlayerBaseA,
            CPlayerCacheData& stPlayerCacheA, CPlayerBaseInfo& stPlayerBaseB,
            CPlayerCacheData& stPlayerCacheB, MsgAnsManorBattle& stAns,
            MsgAnsManorBattle& stObjAns, bool bIsAttackRole, bool bIsAWin,
            int iAttackVal, CDropItem& stDropItemA, CDropItem& stDropItemB);

    int GetMapShow(MsgAnsManorBattle& stAns);

    void UpdateRoleInfo(CPlayerCacheData& stPlayerCache);

    int GetInfo(CPlayerDetailInfo& stPlayerDetail,
            CPlayerCacheData& stPlayerCache, MsgAnsManorBattle& stAns);

    int GetTargetList(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfo& stPlayerDetail, CPlayerCacheData& stPlayerCache,
            CManorBattleInfo& stInfo, MsgAnsManorBattle& stAns);

    int LeaveTargetList(CPlayerDetailInfo& stPlayerDetail,
            CManorBattleInfo& stInfo, MsgAnsManorBattle& stAns);

    void GetDefendTargetList(MsgAnsManorBattle& stAns);

    void GetAttackTargetList(MsgAnsManorBattle& stAns);

    void UpdateTargetList(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfo& stPlayerDetail, CPlayerCacheData& stPlayerCache,
            CManorBattleInfo& stInfo);

    int DeleteTargetListUnit(CPlayerDetailInfo& stPlayerDetail);

    void NotifyTargetList(int iCampTargetChg);

    int DeadRevive(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfo& stPlayerDetail, CPlayerCacheData& stPlayerCache,
            CManorBattleInfo& stInfo, MsgAnsManorBattle& stAns);

    void NotifyAllSceneRole(CManorBattleUnit& stUnit);

    void GetSceneInfo(MsgAnsManorBattle& stAns);

    void GetCommInfo(CPlayerDetailInfo& stPlayerDetail,
            MsgAnsManorBattle& stAns);

    int GetReviveGold(int iDeadTimes);

    void GetRoleInfo(CPlayerCacheData& stPlayerCache, MsgAnsManorBattle& stAns);

    void GetMonsterInfo(int iStatForMon, MsgAnsManorBattle& stAns);

    void GetRank(CPlayerCacheData& stPlayerCache, MsgAnsManorBattle& stAns);

    CManorBattleCfg& GetManorBattleCfg() {return _stCfg;} 

private:
    CManorBattleCfg _stCfg;
    CManorBattleRank _stRank;
    CManorBattleData _stData;
};

#endif //__CManorBattleMgr_h__

